﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EnnErrE.Utils.Numbers
{
    public class Numbers
    {
        public static string Literalize(decimal number)
        {
            String result = string.Empty;
            if (number < 10) // uno - nove
            {
                string[] translation = { "", "uno", "due", "tre", "quattro", "cinque", "sei", "sette", "otto", "nove" };
                result = translation[(int)number];
            }
            else if (number < 20) // undici - diciannove
            {
                string[] translation = { "dieci", "undici", "dodici", "tredici", "quattordici", "quindici", "sedici", "diciassette", "diciotto", "diciannove" };
                result = translation[(int)number - 10];
            }
            else if (number < 100) // venti - novantanove
            {
                string[] translation = { "venti", "trenta", "quaranta", "cinquanta", "sessanta", "settanta", "ottanta", "novanta" };
                result = translation[(int)number / 10 - 2];
                decimal next = number - (int)(number / 10) * 10;
                if (next > 0)
                {
                    string dec = Literalize(next);
                    if ((dec.Length > 0) && ("uo".Contains(dec[0])))
                        result = result.Substring(0, result.Length - 1);
                    result += dec;
                }
            }
            else if (number < 1000) // cento - novecentonovantanove
            {
                decimal cent = (int)number / 100;
                if (cent > 1) result = Literalize(cent);
                result += "cento";
                decimal next = number - (int)(number / 100) * 100;
                if (next > 0) result += Literalize(number - (int)(number / 100) * 100);
            }
            else if (number < 1000000) // mille - novecentonovantanovemilanovecentonovantanove
            {
                decimal mil = (int)(number / 1000);
                result = (mil > 1) ? Literalize(mil) + "mila" : "mille";
                decimal next = number - mil * 1000;
                if (next > 0) result += Literalize(next);
            }
            else if (number < 100000000)
            {
                decimal mil = (int)(number / 1000000);
                result = (mil > 1) ? Literalize(mil) + "milioni" : "unmilione";
                decimal next = number - mil * 1000000;
                if (next > 0) result += Literalize(next);
            }
            else result = "########";

            if (result.Length == 0) result = "zero";
            return result;
        }
    }
}
